import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// 路由前置导航守卫 - 权限控制拦截
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  // 开启进度条
  NProgress.start()
  const token = store.getters.token
  // 有token
  if (token) {
    if (to.path === '/login') {
      // 一旦拦截处理next('/路径'), 路由的后置导航守卫不会执行
      NProgress.done() // 手动关闭进度条
      next('/')
    } else {
      // 判断是否获取过资料
      // if (store.state.user.userInfo.userId)
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      NProgress.done() // 手动关闭进度条
      next('/login')
    }
  }
})
// 路由后置导航守卫 - 进度条关闭
router.afterEach(() => {
  // 关闭进度条
  NProgress.done()
})

